package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.Product;
import com.mostromas.ess.product.req.ProductREQ;
import com.mostromas.ess.product.service.IProductService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 款式详情表-前端控制器
 */
@Api(value = "款式管理接口",description = "款式管理接口，提供款式的增、删、改、查")
@RestController
@RequestMapping("/commodity")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 分页条件查询款式信息
     * @param req
     * @return
     */
    @PreAuthorize(("hasAuthority('commodity:search')"))
    @ApiOperation("根据款号、分类ID、年份ID、季节ID、或品牌ID查询款式列表接口")
    @PostMapping("/search")
    public Result search(@RequestBody ProductREQ req){

        return productService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('commodity:search')"))
    @ApiOperation("查询款式详情接口")
    @ApiImplicitParam(name = "id",value = "款式ID",required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return productService.findDetails(id);
    }

    @PreAuthorize(("hasAuthority('commodity:add')"))
    @ApiOperation("新增款式信息接口")
    @PostMapping
    public Result save(@RequestBody Product product){
        productService.saveProduct(product);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('commodity:edit')"))
    @ApiOperation("修改款式信息接口")
    @PutMapping
    public Result update(@RequestBody Product product){
        productService.updateProduct(product);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('commodity:delete')"))
    @ApiOperation("删除款式信息接口")
    @ApiImplicitParam(name = "id",value = "款式ID",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        productService.deleteProduct(id);
        return Result.ok();
    }
}
